# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     text_area_description
   Description :
   Author :       'li'
   date：          2018/11/14
-------------------------------------------------
   Change Activity:
                   2018/11/14:
-------------------------------------------------
"""
from ml.common.misc.string_util import StringUtility


class TextAreaDescription:
    def __init__(self, content_vector=None, box_coordinate=None, recognition_result=None):
        self.content_vector = content_vector
        self.height, self.width = self.content_vector.shape[0], self.content_vector.shape[1]
        self.box_coordinate = box_coordinate
        self.center_point = None
        self.recognition_result = recognition_result
        self.origin_point_distance = None
        self.textarea_type = None  # 'check_code'、'vertical'、'horizontal'
        self.check_code_threshold_ratio = 3  # h/w

        self._init_parameter()

    def _init_parameter(self):
        self._get_center_point()
        self._detect_textarea_type()

    def _detect_textarea_type(self):
        """
        detect textarea type
        :return:
        """
        if len(self.recognition_result) == 1 and StringUtility.is_integer(self.recognition_result):
            self.textarea_type = 'check_code'
        if self.height >= self.width and len(self.recognition_result) > 1:
            self.textarea_type = 'vertical'
        if self.height < self.width and len(self.recognition_result) > 1:
            self.textarea_type = 'horizontal'

    def _get_center_point(self):
        """
        box_points[1,2,3,4]
        :return:
        """
        assert self.box_coordinate is not None
        w = int((self.box_coordinate[0][0] + self.box_coordinate[2][0]) / 2)
        h = int((self.box_coordinate[0][1] + self.box_coordinate[2][1]) / 2)
        self.center_point = [w, h]

    def get_origin_point_distance(self):
        """
        get distance
        :return:
        """
        if self.origin_point_distance is not None:
            return self.origin_point_distance
        x, y = self.center_point
        self.origin_point_distance = (x ** 2 + y ** 2) ** 5
        return self.origin_point_distance
